import { Controller, Get, Param, UseInterceptors, UsePipes, ValidationPipe } from "@nestjs/common";
import { PostsService } from "./posts.service";
import { Post } from "@prisma/client";
import { FormattedPostWithUser } from "./interfaces/posts.dto";
import { IntValidationPipe } from "src/pipes/int-validation.pipe";
import {  Public } from "src/decorators/is-public.decorator";
import { LoggingInterceptor } from "src/interceptors/logging.interceptor";
import { FineOneParams } from "src/dtos/posts/findOneParams.dto";

@UseInterceptors(LoggingInterceptor)
@Controller("posts")
export class PostsController {
    constructor(private readonly postsService: PostsService) { }

    @Public()
    @Get()
    async findAll(): Promise<Post[]> {
        return await this.postsService.findAll();
    }

     @Get("/raw-all")
    async findAllByRawQuery(): Promise<FormattedPostWithUser[]> {
        return await this.postsService.getAllPostsWithRawQuery();
    }

    @Public()
    @Get("/raw/:id")
    async findPostById(@Param("id",IntValidationPipe) id: number): Promise<Post> {
        try {
            return await this.postsService.getPostWithRawQuery(id);
        } catch (error) {
            throw error;
        }
    }

    @Public()
    @Get("/raw-simple/:id/:userId")
    @UsePipes(new ValidationPipe({ stopAtFirstError:false, whitelist: true })) // 不加这行的话, FineOneParams的验证规则不好用
    async findPostByRawId(@Param() params: FineOneParams): Promise<Post[]> {
        return await this.postsService.getPostById(Number(params.id),Number(params.userId));
    }
}